local tongshu = fk.CreateSkill {
  name = "3s__tongshu",
}

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["3s__tongshu"] = "同戍",
  [":3s__tongshu"] = "结束阶段，你可发动“据守”，将“合鸣”的一项交给一名其他角色，该角色可于其结束阶段可发动“据守”，将“合鸣”的一项交给你。",
  ["#3s__tongshu"] = "你可发动“据守”，将“合鸣”的一项交给一名其他角色",
  ["#3s__tongshu-choose"] = "请选择“合鸣”的一项交给%src",
  ["@[player]tongshu"] = "同戍",
  ["#tongshu-return"] = "你可将“合鸣”的一项还给%src",

  ["3s__jushou"] = "据守",
  [":3s__jushou"] = "结束阶段，你可摸三张牌并翻面。",
}

local function lostheming(player, skill)
   if player:hasSkill("3s__heming") then
     player.room:handleAddLoseSkills(player, "-3s__heming")
     if skill == "3s__heming_slash" then
       player.room:handleAddLoseSkills(player, "3s__heming_jink", "3s__tongshu")
     elseif skill == "3s__heming_jink" then
       player.room:handleAddLoseSkills(player, "3s__heming_slash", "3s__tongshu")
     end
   else
     player.room:handleAddLoseSkills(player, "-"..skill, "3s__tongshu")
   end
end

local function gainheming(player, skill)
   player.room:handleAddLoseSkills(player, skill, "3s__tongshu")
   if player:hasSkill("3s__heming_slash") and player:hasSkill("3s__heming_jink") then
     player.room:handleAddLoseSkills(player, "3s__heming|-3s__heming_slash|-3s__heming_jink", "3s__tongshu")
     if skill == "3s__heming_slash" then
        U.SetSwitchSkillState(player, "3s__heming", fk.SwitchYin)
     end
   end
end

local function giveheming(from, to)
  local choices = {}
  if from:hasSkill("3s__heming") then
    choices = {"3s__heming_slash", "3s__heming_jink"}
  end
  if from:hasSkill("3s__heming_slash") then
    table.insertIfNeed(choices, "3s__heming_slash")
  end
  if from:hasSkill("3s__heming_jink") then
    table.insertIfNeed(choices, "3s__heming_jink")
  end
  local choice = from.room:askToChoice(from, {
    choices = choices,
    skill_name = "3s__tongshu",
    prompt = "#3s__tongshu-choose:"..to.id,
    all_choices = {"3s__heming_slash", "3s__heming_jink"},
   })
   lostheming(from, choice)
   gainheming(to, choice)
end

tongshu:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(tongshu.name) and (player == target or table.contains(player:getTableMark("@[player]tongshu"), target.id))
    and (player:hasSkill("3s__heming") or player:hasSkill("3s__heming_slash") or player:hasSkill("3s__heming_jink"))
    and target.phase == Player.Finish
  end,
  on_cost = function(self, event, target, player, data)
    if target == player and player:hasSkill(tongshu.name) then
        local to = player.room:askToChoosePlayers(player, {
        targets = player.room:getOtherPlayers(player),
        max_num = 1,
        min_num = 1,
        skill_name = tongshu.name,
        cancelable = true,
        prompt = "#3s__tongshu",
        })
        if #to > 0 then
          event:setCostData(self, to[1])
          return true
        end
    else
      return player.room:askToSkillInvoke(target, {skill_name = tongshu.name, prompt = "#tongshu-return:"..player.id})
    end
  end,
  on_use = function(self, event, target, player, data)
    local to = player
    if player:hasSkill(tongshu.name) and target == player then
      to = event:getCostData(self)
    end
    target:drawCards(3, "jushou")
    target:turnOver()
    giveheming(target, to)
    if player:hasSkill(tongshu.name) and target == player then
      player.room:addTableMarkIfNeed(player, "@[player]tongshu", to.id)
    end
    if not target:hasSkill("3s__heming") and not target:hasSkill("3s__heming_slash") and not target:hasSkill("3s__heming_jink") then
      player.room:removeTableMark(player, "@[player]tongshu", target.id)
    end
  end,
})

return tongshu
